import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime

# 邮件服务器设置
SMTP_SERVER = 'smtp.qq.com'  # 替换为你的 SMTP 服务器
SMTP_PORT = 465  # 替换为你的 SMTP 端口
SMTP_USERNAME = '1517637121@qq.com'  # 替换为你的邮箱
SMTP_PASSWORD = 'grlbkowlgtmgiffi'  # 替换为你的邮箱密码

# 邮件内容设置
EMAIL_FROM = '1517637121@qq.com'  # 替换为你的邮箱
EMAIL_TO = '1517637121@qq.com'  # 替换为收件人的邮箱
EMAIL_SUBJECT = '每日报告'

# 生成邮件内容
def generate_email_body():
    # 你可以在这里生成你的报告内容
    today = datetime.now().strftime("%Y-%m-%d")
    email_body = f"这是 {today} 的每日报告。"
    return email_body

# 发送邮件
def send_email():
    msg = MIMEMultipart()
    msg['From'] = EMAIL_FROM
    msg['To'] = EMAIL_TO
    msg['Subject'] = EMAIL_SUBJECT

    body = generate_email_body()
    msg.attach(MIMEText(body, 'plain'))

    try:
        server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        server.set_debuglevel(1)  # 打开调试输出
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(SMTP_USERNAME, SMTP_PASSWORD)
        text = msg.as_string()
        server.sendmail(EMAIL_FROM, EMAIL_TO, text)
        server.quit()
        print("邮件发送成功")
    except smtplib.SMTPAuthenticationError as e:
        print(f"身份验证失败: {e}")
        import traceback
        traceback.print_exc()
    except smtplib.SMTPConnectError as e:
        print(f"连接失败: {e}")
        import traceback
        traceback.print_exc()
    except smtplib.SMTPServerDisconnected as e:
        print(f"服务器断开连接: {e}")
        import traceback
        traceback.print_exc()
    except smtplib.SMTPException as e:
        print(f"SMTP 异常: {e}")
        import traceback
        traceback.print_exc()
    except Exception as e:
        print(f"发送邮件失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    send_email()
